//
//  AnswerSheetCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/5.
//

#import "AnswerSheetCell.h"

@interface AnswerSheetCell()

@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIView *answerView;

@end

@implementation AnswerSheetCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = JPUIFontPingFangSCRegular(14);
    self.titleLabel.textColor = KFontGrayColor;
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.height.mas_equalTo(@20);
        make.top.mas_equalTo(@5);
        make.width.mas_equalTo(@43);
    }];
    
    self.answerView = [[UIView alloc]init];
    self.answerView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.answerView];
    [self.answerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@69);
        make.right.mas_equalTo(@-16);
        make.top.bottom.mas_equalTo(@0);
    }];
    
}


-(void)btnClick:(UIButton*)button{
    NSInteger index = button.tag-100;
    if(_clickBlock){
        _clickBlock(_listModel.examRecords[index]);
    }
}

-(void)setListModel:(ExamPaperAnswerSheetListModel *)listModel{
    _listModel = listModel;
    self.titleLabel.text = listModel.name;
    [self.answerView removeAllSubviews];
    CGFloat mid = (self.currentAvailableWidth-69-16-30*6)/5.0;
    for(NSInteger i = 0;i < listModel.examRecords.count;i++){
        ExamPaperAnswerSheetRecordModel *recordModel = listModel.examRecords[i];
        UIButton *btn = [[UIButton alloc]init];
        [btn setTitle:recordModel.sort forState:UIControlStateNormal];
        [btn setTitleColor:KFontBlackColor forState:UIControlStateNormal];
        btn.titleLabel.font = JPUIFontInterMedium(14);
        btn.layer.cornerRadius = 15;
        btn.layer.masksToBounds = YES;
        btn.layer.borderWidth = 0.5;
        if((recordModel.isDone&&[recordModel.isDone isEqualToString:@"1"])||(self.testType == 2)||(self.testType == 3)){
            //已经答题 有结果
            if(recordModel.correct&&recordModel.correct.integerValue == 1){
                btn.layer.borderColor = KThemeColor.CGColor;
                [btn setTitleColor:KThemeColor forState:UIControlStateNormal];
            }else{
                if(recordModel.correct){
                    btn.layer.borderColor = UIColorHex(0xFE4A4B).CGColor;
                    [btn setTitleColor:UIColorHex(0xFE4A4B) forState:UIControlStateNormal];
                }else{
                    btn.layer.borderColor = [UIColor whiteColor].CGColor;
                }
            }
        }else if(recordModel.isDone&&[recordModel.isDone isEqualToString:@"0"]){
            btn.layer.borderColor = KFontGrayColor.CGColor;
        }else{
            btn.layer.borderColor = [UIColor whiteColor].CGColor;
        }
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.answerView addSubview:btn];
        NSInteger r = i/6;
        NSInteger c = i%6;
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(c*(30+mid));
            make.top.mas_equalTo(r*(30+22));
            make.width.height.mas_equalTo(@30);
        }];
    }
}


@end
